package com.thinkit.bigdata.web.thread;

import com.thinkit.bigdata.web.model.bus.BusVoiceDetails;
import com.thinkit.bigdata.web.service.bus.BusVoiceDetailsService;
import org.apache.log4j.Logger;

import java.util.concurrent.BlockingQueue;

/**
 * 消费者线程
 *
 * @author shijiakai
 */
public class Consumer implements Runnable {

    private BlockingQueue<BusVoiceDetails> queue;

    private BusVoiceDetailsService detailsService;

    private static Logger logger = Logger.getLogger(Consumer.class);

    //构造函数
    public Consumer(BlockingQueue<BusVoiceDetails> queue, BusVoiceDetailsService detailsService) {
        this.queue = queue;
        this.detailsService = detailsService;
    }

    public void run() {
        String VOICE_LOG_MES = "【异步导出数据任务】";
        logger.info(VOICE_LOG_MES + "启动消费者线程！");
        boolean isRunning = true;
        BusVoiceDetails details;
        boolean isExport;
        while (isRunning) {
            try {
                logger.info(VOICE_LOG_MES + "正从队列获取数据...");
                details = queue.take();
                logger.info(VOICE_LOG_MES + "任务 = " + details.toString());
                //todo进行任务处理 -- 分批下载数据写成xls
                if ("voiceExport".equals(details.getDatacode())) {
                    isExport = detailsService.exportVoiceXlsPage(details);
                } else if ("5GExport".equals(details.getDatacode())) {
                    isExport = detailsService.export5GXlsPage(details);
                } else {
                    isExport = false;
                }
                logger.info(VOICE_LOG_MES + "导出文件 : " + isExport);
            } catch (InterruptedException e) {
                logger.info(VOICE_LOG_MES + "获取taskId失败！");
            } finally {
                logger.info(VOICE_LOG_MES + "退出消费者线程！");
            }
        }
    }
}
